jQuery(document).ready(function($){
	var $form_modal = $('.cd-user-modal'),
		$form_login = $form_modal.find('#cd-login'),
		$form_signup = $form_modal.find('#cd-signup'),
		$form_forgot_password = $form_modal.find('#cd-reset-password'),
		$form_modal_tab = $('.cd-switcher'),
		$tab_login = $form_modal_tab.children('li').eq(0).children('a'),
		$tab_signup = $form_modal_tab.children('li').eq(1).children('a'),
		$forgot_password_link = $form_login.find('.cd-form-bottom-message a'),
		$back_to_login_link = $form_forgot_password.find('.cd-form-bottom-message a'),
		$main_nav = $('.login-signup');
	//open modal	打开登录注册
	$main_nav.on('click', function(event){

		if( $(event.target).is($main_nav) ) {
			// on mobile open the submenu	打开登录注册子菜单
			$(this).children('ul').toggleClass('is-visible');
		} else {
			// on mobile close submenu  关闭登录注册子菜单
			$main_nav.children('ul').removeClass('is-visible');
			//show modal layer	显示模型层
			$form_modal.addClass('is-visible');	
			//show the selected form	显示选择表单
			( $(event.target).is('.cd-signup') ) ? signup_selected() : login_selected();
		}

	});

	//close modal
	$('.cd-user-modal').on('click', function(event){
		if( $(event.target).is($form_modal) || $(event.target).is('.cd-close-form') ) {
			$form_modal.removeClass('is-visible');
			$form_login.find('input[type="text"]').removeClass('has-error').siblings('span').removeClass('is-visible');
			$form_signup.find('input[type="text"]').removeClass('has-error').siblings('span').removeClass('is-visible');
		}	
	});
	//close modal when clicking the esc keyboard button
	$(document).keyup(function(event){
    	if(event.which=='27'){
    		$form_modal.removeClass('is-visible');
    		$form_login.find('input[type="text"]').removeClass('has-error').siblings('span').removeClass('is-visible');
    		$form_signup.find('input[type="text"]').removeClass('has-error').siblings('span').removeClass('is-visible');
	    }
    });

	//switch from a tab to another
	$form_modal_tab.on('click', function(event) {
		event.preventDefault();
		( $(event.target).is( $tab_login ) ) ? login_selected() : signup_selected();
	});
 
	//hide or show password
	$('.hide-password').on('click', function(){
		var $this= $(this),
			$password_field = $this.prev('input');
		
		( 'password' == $password_field.attr('type') ) ? $password_field.attr('type', 'text') : $password_field.attr('type', 'password');
		( '隐藏' == $this.text() ) ? $this.text('显示') : $this.text('隐藏');
		//focus and move cursor to the end of input field
		$password_field.putCursorAtEnd();
	});
	
	// $('.hide-password').on('click', function(){
	// 	var $this= $(this),
	// 		$password_field = $this.prev('input');
	// 	alert($('#eyes').css("display") == 'inline-block');
	// 	( 'password' == $password_field.attr('type') ) ? $password_field.attr('type', 'text') : $password_field.attr('type', 'password');
	// 	( $('#eyes').css("display") == 'inline-block'  ) ?( $('#eyes').css("display","none") && $('#eyesclose').css("display","inline-block") ): ($('#eyes').css("display","inline-block") && $('#eyesclose').css("display","none") );
	// 	
	// 	//focus and move cursor to the end of input field
	// 	$password_field.putCursorAtEnd();
	// });
	

	//show forgot-password form 
	$forgot_password_link.on('click', function(event){
		event.preventDefault();
		forgot_password_selected();
	});

	//back to login from the forgot-password form
	$back_to_login_link.on('click', function(event){
		event.preventDefault();
		login_selected();
	});

	function login_selected(){
		$form_login.addClass('is-selected');
		$form_signup.removeClass('is-selected');
		$form_forgot_password.removeClass('is-selected');
		$tab_login.addClass('selected');
		$tab_signup.removeClass('selected');
	}

	function signup_selected(){
		$form_login.removeClass('is-selected');
		$form_signup.addClass('is-selected');
		$form_forgot_password.removeClass('is-selected');
		$tab_login.removeClass('selected');
		$tab_signup.addClass('selected');
	}

	function forgot_password_selected(){
		$form_login.removeClass('is-selected');
		$form_signup.removeClass('is-selected');
		$form_forgot_password.addClass('is-selected');
	}

	//点击登录按钮出现错误提示，再次点击移除
	/*$form_login.find('input[type="submit"]').on('click', function(event){
		event.preventDefault();
		$form_login.find('input[type="text"]').toggleClass('has-error').siblings('span').toggleClass('is-visible');	
	});*/
	//点击登录按钮出现错误提示，再次点击移除
//	$form_login.find('input[type="submit"]').on('click', function(event){
//		event.preventDefault();
//		if ($form_login.find('input[type="text"]').hasClass('has-error')) {
//			$form_login.find('input[type="text"]').removeClass('has-error').siblings('span').removeClass('is-visible');
//		} else {
//		$form_login.find('input[type="text"]').addClass("has-error").siblings('span').addClass('is-visible');
//	}
//	});
	//有错误提示时点击文本框移除
	$form_login.find('input[type="text"]').on('click',function(event){
		if ($form_login.find('input[type="text"]').hasClass('has-error')) {
			$form_login.find('input[type="text"]').removeClass('has-error').siblings('span').removeClass('is-visible');
		}
	});
	
	//点击注册按钮判断手机号是否正确
	$form_signup.find('input[type="submit"]').on('click', function(event){
		if($form_signup.find('input[type="checkbox"]').is(':checked') == true){
			event.stopPropagation();
		}
	});
});


//credits http://css-tricks.com/snippets/jquery/move-cursor-to-end-of-textarea-or-input/
jQuery.fn.putCursorAtEnd = function() {
	return this.each(function() {
    	// If this function exists...
    	if (this.setSelectionRange) {
    		
      		// ... then use it (Doesn't work in IE)
      		// Double the length because Opera is inconsistent about whether a carriage return is one character or two. Sigh.
      		var len = $(this).val().length * 2;
      		this.setSelectionRange(len, len);
    	} else {
    		// ... otherwise replace the contents with itself
    		// (Doesn't work in Google Chrome)
      		$(this).val($(this).val());
    	}
	});
};